/*
 * @Author: LVGRAPE
 * @LastEditors: LVGRAPE
 */
#include <windows.h>
#include "dsp_test.h"

#define WIDTH SAMPLE_COUNT
#define HEIGHT 400
#define PI 3.14159265358979323846
#define x_apm 100
#define y_apm 100

extern void ware_generate(void);
// 窗口过程函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        // 设置绘图模式为抗锯齿
        SetBkMode(hdc, TRANSPARENT);

        // 定义4种不同颜色的画笔
        HPEN hPens[4];
        hPens[0] = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));   // 红色
        hPens[1] = CreatePen(PS_SOLID, 2, RGB(0, 255, 0));   // 绿色
        hPens[2] = CreatePen(PS_SOLID, 2, RGB(0, 0, 255));   // 蓝色
        hPens[3] = CreatePen(PS_SOLID, 2, RGB(255, 0, 255)); // 黄色

        HBRUSH hBrush = (HBRUSH)GetStockObject(NULL_BRUSH);

        // 绘制坐标轴
        MoveToEx(hdc, 0, HEIGHT / 2, NULL);
        LineTo(hdc, WIDTH, HEIGHT / 2);
        MoveToEx(hdc, 0, 0, NULL);
        LineTo(hdc, 0, HEIGHT);

        // 定义4条曲线的相位
        double phases[4] = {0, 0, 0, 0};
        float *data[4] = {ware1, ware2, ware3, ware4};
        // 绘制4条曲线
        for (int curveIndex = 0; curveIndex < 4; curveIndex++)
        {
            SelectObject(hdc, hPens[curveIndex]);
            SelectObject(hdc, hBrush);

            for (int x = 0; x < WIDTH; x+= x_apm)
            {
                double yValue = data[curveIndex][x/x_apm];
                int y = (int)(HEIGHT / 2 + yValue*y_apm);
                if (x == 0)
                {
                    MoveToEx(hdc, 0, HEIGHT/2, NULL);
                    LineTo(hdc, x, y);
                }
                else
                {
                    LineTo(hdc, x, y);
                }
            }
        }

        // 删除画笔
        for (int i = 0; i < 4; i++)
        {
            DeleteObject(hPens[i]);
        }
        DeleteObject(hBrush);
        EndPaint(hwnd, &ps);
        break;
    }
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

// 主函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    // 注册窗口类
    WNDCLASS wc = {0};
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = "SineWaveWindow";
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    RegisterClass(&wc);

    // 创建窗口
    HWND hwnd = CreateWindow(wc.lpszClassName, "dsp test", WS_OVERLAPPEDWINDOW,
                             CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT, NULL, NULL, hInstance, NULL);
    ware_generate();
    // 显示窗口
    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    // 消息循环
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
